package com.cc.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.hosp.service.HospitalSetService;
import com.cc.yygh.common.result.Result;
import com.cc.yygh.common.utils.MD5;
import com.cc.yygh.model.hosp.HospitalSet;
import com.cc.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin  //允许跨域
public class HospitalSetController {


    //注入service
    @Autowired
    private HospitalSetService setService;

    //1.查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result finAll(){
        List<HospitalSet> list = setService.list();
        return Result.ok(list);
    }

    //2.删除医院设置
    @ApiOperation(value = "删除医院设置信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        System.out.println("调用方法");
        boolean flag = setService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //3.条件查询加分页
    @ApiOperation(value = "条件查询")
    @PostMapping("findPageHospSet/{current}/{limit}")//post提交才能得到RequestBody传的值
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        IPage<HospitalSet> pageHospitalSet = setService.page(page, wrapper);

        //返回结果
        return Result.ok(pageHospitalSet);
    }


    //4.添加
    @ApiOperation(value = "添加")
    @PostMapping("saveHospitalSet")//它这里就是提供了Hset属性，但是签名密钥要电脑自动输入
     public Result saveHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet){
        //设置状态 1 使用，0不能使用
         hospitalSet.setStatus(1);
         //签名密钥
         Random random = new Random();
         hospitalSet.setSignKey( MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
         boolean flag = setService.save(hospitalSet);
         if (flag){
             return Result.ok();
         }else {
             return Result.fail();
         }

     }


    //5.根据id查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = setService.getById(id);
        if (hospitalSet!=null){
            return Result.ok(hospitalSet);
        }else {
            return Result.fail();
        }
    }

    //6.修改
    @ApiOperation(value = "修改")
    @PostMapping("updateHospitalSet")//我giao，这也太方便了吧，全都封装好了
    public Result updateHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet){

        boolean flag = setService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    //7.批量删除
    @ApiOperation(value = "批量删除")
    @DeleteMapping("deleteHospitalSet")
    public Result deleteHospitalSet(@RequestBody List<Long> list){
        boolean flag = setService.removeByIds(list);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //医院锁定
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet = setService.getById(id);
        hospitalSet.setStatus(status);
        System.out.println("调用方法");
        setService.updateById(hospitalSet);
        return Result.ok();
    }
    
    //签名密钥
    @PutMapping("sendKey/{id}")
    public Result singHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = setService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return  Result.ok();
    }
}
